<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组别名称" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入组别主键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="villageName">
        <el-input
          v-model="queryParams.villageName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCode">
        <el-input
          v-model="queryParams.idCode"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户主关系" prop="householderRelationship">
        <el-input
          v-model="queryParams.householderRelationship"
          placeholder="请输入户主关系"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否党员" prop="partyMember">
        <el-select v-model="queryParams.partyMember" placeholder="请选择是否党员" clearable size="small">
          <el-option
            v-for="dict in partyMemberOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
       <el-form-item label="本地人员" prop="sex">
        <el-select v-model="queryParams.villageSort" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in sortOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职务" prop="postType">
        <el-select v-model="queryParams.postType" placeholder="请选择职务" clearable size="small">
          <el-option
            v-for="dict in postTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basicdata:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basicdata:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basicdata:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['basicdata:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="组别名称" align="center" prop="groupName" />
      <el-table-column label="姓名" align="center" prop="villageName" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="idCode" />
      <el-table-column label="户主关系" align="center" prop="householderRelationship" />
      <el-table-column label="户别" align="center" prop="householdType" />
      <el-table-column label="是否党员" align="center" prop="partyMember" :formatter="partyMemberFormat" />
      <el-table-column label="职务" align="center" prop="postType" :formatter="postTypeFormat" />
      <el-table-column label="本地人员" align="center" prop="villageSort" :formatter="sortFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basicdata:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basicdata:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改村民信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="组别名称" prop="groupId">
          <el-select v-model="form.groupId" placeholder="请选择组别" filterable clearable>
            <el-option
              v-for="dict in groupOptions"
              :key="dict.id"
              :label="dict.groupName"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="villageName">
          <el-input v-model="form.villageName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable size="small"
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCode">
          <el-input v-model="form.idCode" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="户主关系" prop="householderRelationship">
          <el-input v-model="form.householderRelationship" placeholder="请输入户主关系" />
        </el-form-item>
        <el-form-item label="户别" prop="householdType">
          <el-input v-model="form.householdType" placeholder="请输入户别" />
        </el-form-item>
        <el-form-item label="是否党员" prop="partyMember">
          <el-select v-model="form.partyMember" placeholder="请选择是否党员">
            <el-option
              v-for="dict in partyMemberOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="本地人员" prop="villageSort">
          <el-select v-model="form.villageSort" placeholder="请选择">
            <el-option
              v-for="dict in sortOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职务" prop="postType">
          <el-select v-model="form.postType" placeholder="请选择职务">
            <el-option
              v-for="dict in postTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/basicdata/villager";
import { groupList } from "@/api/basicdata/group";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 村民信息表格数据
      infoList: [],
       //组别信息列表
      groupOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别字典
      sexOptions: [],
      // 是否党员字典
      partyMemberOptions: [],
      // 是否是外地人员字典
      sortOptions: [],
      // 职务类型(0村民，1组长，2区长)字典
      postTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
        villageName: null,
        sex: null,
        phone: null,
        birthday: null,
        idCode: null,
        householderRelationship: null,
        householdType: null,
        partyMember: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        villageName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("hxc_party_member").then(response => {
      this.partyMemberOptions = response.data;
    });
    this.getDicts("hxc_local_type").then(response => {
      this.sortOptions = response.data;
    });
      this.getDicts("hxc_post_type").then(response => {
          this.postTypeOptions = response.data;
      });
    this.getGroupList();
  },
  methods: {
    //获取组别列表
    getGroupList(){
      groupList().then(response => {
        this.groupOptions = response.data;
      });
    },
    /** 查询村民信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 是否党员字典翻译
    partyMemberFormat(row, column) {
      return this.selectDictLabel(this.partyMemberOptions, row.partyMember);
    },
    // 职务类型(0村民，1组长，2区长)字典翻译
    postTypeFormat(row, column) {
        return this.selectDictLabel(this.postTypeOptions, row.postType);
    },
    //是否外地人员字典翻译
    sortFormat(row, column){
      return this.selectDictLabel(this.sortOptions, row.villageSort);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        groupId: null,
        villageName: null,
        sex: null,
        phone: null,
        birthday: null,
        idCode: null,
        householderRelationship: null,
        householdType: null,
        partyMember: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加村民信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改村民信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除村民信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有村民信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
